﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace GameNode
{
    class Node
    {
        public int X { get; set; }
        public int Y { get; set; }
        private Random rnd;

        public Node(int x, int y)
        {
            X = x;
            Y = y;
            rnd = new Random();
            Thread.Sleep(150);
        }

        protected void MoveUp()
        { Y--; }

        protected void MoveDown()
        { Y++; }

        protected void MoveLeft()
        { X--; }

        protected void MoveRight()
        { X++; }

        public bool IsOnPossition(int positionX, int positionY)
        {
            if (X == positionX && Y == positionY)
                return true;
            return false;
        }

        public virtual void Move(Func<int, int, Position> positionStatus)
        {
            int randomMove = rnd.Next(1, 5);
            switch (randomMove)
            {
                case 1:
                    {
                        //MoveUp
                        if (positionStatus(X, Y - 1) == Position.Free)
                            //IsPositionFree(node.X, node.Y - 1) && gameField.IsOnField(node.X, node.Y - 1))
                            MoveUp();
                    }
                    break;

                case 2:
                    {
                        //MoveDown
                        if (positionStatus(X, Y + 1) == Position.Free)
                            //IsPositionFree(node.X, node.Y + 1) &&
                            //gameField.IsOnField(node.X, node.Y + 1))
                            MoveDown();
                    }
                    break;

                case 3:
                    {
                        //MoveLeft
                        if (positionStatus(X - 1, Y) == Position.Free)
                            //IsPositionFree(node.X - 1, node.Y) &&
                            //gameField.IsOnField(node.X - 1, node.Y))
                            MoveLeft();
                    }
                    break;

                case 4:
                    {
                        //MoveRight
                        if (positionStatus(X + 1, Y) == Position.Free)
                            //IsPositionFree(node.X + 1, node.Y) &&
                            //gameField.IsOnField(node.X + 1, node.Y))
                            MoveRight();
                    }
                    break;
            }
        }
    }

    class ConsoleNode : Node
    {
        public char NodeSymbol { get; private set; }
        public ConsoleColor NodeColor { get; private set; }

        public ConsoleNode(int x, int y, char nodeSymbol, ConsoleColor nodeColor)
            : base(x, y)
        {
            NodeSymbol = nodeSymbol;
            NodeColor = nodeColor;
        }

    }

    class MaksimGameNode : ConsoleNode
    {

        public MaksimGameNode(int x, int y, char nodeSymbol, ConsoleColor nodeColor) : base(x, y, nodeSymbol, nodeColor)
        {

        }
        public override void Move(Func<int, int, Position> positionStatus)
        {
            if (positionStatus(X, Y + 1) == Position.Free)
                MoveDown();
            else if (positionStatus(X, Y + 1) == Position.Border)
            {
                if (positionStatus(X + 1, Y) == Position.Free)
                    MoveRight();
            }
        }

    }

    class SmartNode : ConsoleNode
    {
        private bool maxUp;
        private bool maxLeft;
        private bool direction; //false - moving right; true = moving left

        public SmartNode(int x, int y, char nodeSymbol, ConsoleColor nodeColor) : base(x, y, nodeSymbol, nodeColor)
        {

        }

        public override void Move(Func<int, int, Position> positionStatus)
        {
            if (!maxUp)
            {
                if (positionStatus(X, Y - 1) == Position.Free)
                    MoveUp();
                else if (positionStatus(X, Y - 1) == Position.Border)
                    maxUp = true;
            }
            else if (!maxLeft)
            {
                if (positionStatus(X - 1, Y) == Position.Free)
                    MoveLeft();
                else if (positionStatus(X - 1, Y) == Position.Border)
                    maxLeft = true;
            }
            else if (maxUp && maxLeft)
            {
                if (!direction)
                {
                    if (positionStatus(X + 1, Y) == Position.Free)
                        MoveRight();
                    else if (positionStatus(X + 1, Y) == Position.Border)
                    {
                        if(positionStatus(X, Y+1) == Position.Free)
                        {
                            MoveDown();
                            direction = !direction;
                        }
                    }
                }
                else if (direction)
                {
                    if (positionStatus(X - 1, Y) == Position.Free)
                        MoveLeft();
                    else if (positionStatus(X - 1, Y) == Position.Border)
                    {
                        if (positionStatus(X, Y + 1) == Position.Free)
                        {
                            MoveDown();
                            direction = !direction;
                        }
                    }

                }

            }
            //else if (!direction)
            //{
            //    if (positionStatus(X, Y + 1) == Position.Free)
            //        MoveDown();
            //}









        }

    }
}
